package com.shaoshaohuo.app.view;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.PopupWindow;
import java.util.ArrayList;
import java.util.List;
import com.shaoshaohuo.app.R;

/**
 * Created by yusonglin on 2015/7/14. Description:
 */
public class CarVolumePopwindow implements View.OnClickListener {

    private Context context;
    private PopupWindow hintPopWinwow;

    private WheelView mWheelView1;
    private WheelView mWheelView2;
    private WheelView mWheelView3;

    private Button mCancelButton;
    private Button mOkButton;

    private OnSelectListener onSelectListener;

    public CarVolumePopwindow(Context context) {
        this.context = context;
        initView();
    }

    private void initView() {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final View contentView = inflater.inflate(R.layout.popwindow_layout_bottom_select, null, false);
        hintPopWinwow = new PopupWindow(contentView, WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT, true);
        hintPopWinwow.setOutsideTouchable(true);
        hintPopWinwow.setBackgroundDrawable(new BitmapDrawable());
        hintPopWinwow.setAnimationStyle(R.style.Anim_Dialog_Bottom);

        mWheelView1 = (WheelView) contentView.findViewById(R.id.wv1);
        mWheelView2 = (WheelView) contentView.findViewById(R.id.wv2);
        mWheelView3 = (WheelView) contentView.findViewById(R.id.wv3);

        mOkButton = (Button) contentView.findViewById(R.id.button_ok);
        mCancelButton = (Button) contentView.findViewById(R.id.button_cancel);

        mWheelView1.setClumnCount(3);
        mWheelView2.setClumnCount(3);
        mWheelView3.setClumnCount(3);

        mOkButton.setOnClickListener(this);
        mCancelButton.setOnClickListener(this);

        List<String> initList = initList();
        setFirst(initList);
        setSecond(initList);
        setThird(initList);
    }

    private List<String> initList() {
        List<String> list = new ArrayList<>();
        for (int i = 1; i <= 30; i++) {
            list.add(i+"米");
        }
        return list;
    }

    private void setFirst(List<String> list) {
        List<Object> items = new ArrayList<>();
        for (String p : list) {
            items.add(p);
        }
        mWheelView1.setItems(items);
    }

    private void setSecond(List<String> list) {
        List<Object> items = new ArrayList<>();
        for (String p : list) {
            items.add(p);
        }
        mWheelView2.setItems(items);
    }

    private void setThird(List<String> list) {
        List<Object> items = new ArrayList<>();
        for (String p : list) {
            items.add(p);
        }
        mWheelView3.setItems(items);
    }

    public boolean isShowing() {
        return hintPopWinwow.isShowing();
    }

    public void showLocation(View view) {
        hintPopWinwow.showAtLocation(view, Gravity.BOTTOM, 0, 0);
    }

    public String getSelectStr() {
        Object one = mWheelView1.getSeletedItem();
        Object two = mWheelView2.getSeletedItem();
        Object three = mWheelView3.getSeletedItem();
        StringBuilder builder = new StringBuilder();
        if (one != null) {
            builder.append(((int) one / 100.0) + "*");
        }
        if (two != null) {
            builder.append(((int) two / 100.0) + "*");
        }
        if (three != null) {
            builder.append(((int) three / 100.0) + "*");
        }
        builder.append("m");
        return builder.toString();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
        case R.id.button_ok:
            if (onSelectListener != null) {
                int areaId = -1;
                Object one = mWheelView1.getSeletedItem();
                Object two = mWheelView2.getSeletedItem();
                Object three = mWheelView3.getSeletedItem();
                StringBuilder builder = new StringBuilder();
                onSelectListener.onSelect((int) one, (int) two, (int) three, getSelectStr());
            }
            hintPopWinwow.dismiss();
            break;
        case R.id.button_cancel:
            hintPopWinwow.dismiss();
            break;
        }
    }

    public void setOnSelectListener(OnSelectListener onSelectListener) {
        this.onSelectListener = onSelectListener;
    }

    public interface OnSelectListener {

        void onSelect(int ont, int two, int three, String str);
    }

}
